function UpdateRegion(decription,reference,gl,tag_id)
{
	var service;
	if (!map) 
		service = new google.maps.places.PlacesService(document.createElement('div'));
	else
		service = new google.maps.places.PlacesService(map);
	var request = {reference: reference };
    service.getDetails(request, function(place, status) {
		if (status == google.maps.places.PlacesServiceStatus.OK) {
			if (document.URL.toLowerCase().indexOf('item.php') < 0) {
				if (map && (tag_id.toUpperCase() != 'REGSEL1')) {
					map.fitBounds(place.geometry.viewport);
					ShowClusters(place.geometry.viewport);
				}
			}
		
			var Sel = document.getElementById(tag_id);
			if (Sel) {
				
				Sel.innerHTML = place.formatted_address;
				
				Sel = document.getElementById(tag_id + '_' + 'formatted_addr'); if (Sel) Sel.value = '';
				Sel = document.getElementById(tag_id + '_' + 'country'); if (Sel) Sel.value = '';
				Sel = document.getElementById(tag_id + '_' + 'administrative_area_level_1'); if (Sel) Sel.value = '';
				Sel = document.getElementById(tag_id + '_' + 'administrative_area_level_2'); if (Sel) Sel.value = '';
				Sel = document.getElementById(tag_id + '_' + 'locality'); if (Sel) Sel.value = '';
				
				
				
				Sel = document.getElementById(tag_id + '_' + 'PlaceRef');
				if (Sel) {
					Sel.value = reference;
					Sel = document.getElementById(tag_id + '_' + 'formatted_addr');
					Sel.value = place.formatted_address;
					
					for (var i = 0, addrPart; addrPart = place.address_components[i]; i++) {
					
						Sel = document.getElementById(tag_id + '_' + addrPart.types[0]);
						if (Sel) Sel.value = addrPart.long_name;

					}
				}
				Sel = document.getElementById(tag_id + '_' + 'viewport');
				if (Sel) Sel.value = place.geometry.viewport.toString();
				Sel = document.getElementById(tag_id + '_' + 'formatted_addr'); 
				if (Sel) Sel.value = place.formatted_address;
				
			}
		  
		  
			if (gl == '1') {
				deleteCookie('geolocation_viewport');
				deleteCookie('geolocation_location');
				setCookie('geolocation_viewport',place.geometry.viewport.toString(),{ expires: 7776000 });
				setCookie('geolocation_location',place.geometry.location.toString(),{ expires: 7776000 });
			}
		}
	});		
	var regionSelect = document.getElementById(tag_id);
	regionSelect.innerHTML = decription;
	if (gl == '1') {
		deleteCookie('geolocation_place');
		setCookie('geolocation_place',decription,{ expires: 7776000 });
	}
}



function GenListRes(gl,tag_id)
{
    var options =
        {
            level:    '(regions)',   // level (region | subregion | city)
            popup_id: 'address-selector',   // dialog id
            //APIkey:  'AIzaSyCIqU8oTkfB-VLHmD-fih1T8srnv3xeM-o',
			lang: 'ru',
			sensor: 'true'
        };

	function callback(predictions, status) {
		var results = document.div_for_resp;
		var str;
		results.innerHTML = '<br>';
		if (status != google.maps.places.PlacesServiceStatus.OK) {
			//alert(status);
			results.innerHTML += '<li>ничего не найдено...</li>';
			return;
		}
		for (var i = 0, prediction; prediction = predictions[i]; i++) {
			str = "'"+prediction.description+"','"+prediction.reference+"','"+gl+"','"+tag_id+"'";
			results.innerHTML += '<li><a href="#" onmousedown="UpdateRegion('+str+')">' + prediction.description + '</a></li>';
		}
	};


	//var div_for_resp = document.div_for_resp;
	var val = document.getElementById(options.popup_id +'_inp').value;
	if (val != '') {
	  var service = new google.maps.places.AutocompleteService();
	  service.getPlacePredictions({input: val, types: [options['level']], language: options['lang'], sensor: options['sensor']}, callback);
  }
}

function SelectUserRegion(tag_id,gl)
{
	if (document.address_selector)
		document.address_selector.closeSelector();

    var options =
        {
            popup_id: 'address-selector'   // dialog id
        };

	var div_for_resp = document.createElement('div');
		div_for_resp.setAttribute('style','height: auto; border: 0px; list-style: none; ');


    // Get scroll position
    /*
	var getScroll = function()
    {
        var x = 0, y = 0;
        var el = document.getElementById(tag_id);
		var obj = new Object();
        obj.x = el.offsetLeft;
        obj.y = el.offsetTop;
        return obj;
    };
	*/

	
    var centerElement = function(e)
    {
        /*
		var scroll = getScroll();
        e.style.top = scroll.y+10+'px';
		e.style.left = scroll.x+'px';
		*/
		var tag_el = document.getElementById(tag_id);
		var tag_pos = getOffset(tag_el);
		e.style.top = tag_pos.top+'px';
		e.style.left = tag_pos.left+e.scrollWidth+'px';
		
    }
	
	




		

    /*
     * Create wrapper
     */
    var createWrapper = function()
    {
        if (document.address_selector)
            return document.address_selector;

        // find active element. for add div
        var element = document.getElementsByTagName("BODY")[0]; // for all

        // wrapper
        var wrapper = document.createElement('div');
        wrapper.id = options.popup_id;
		
        element.appendChild(wrapper);
		
		var div_border = document.createElement('div');
		div_border.setAttribute('class','search_style');
		
		var input_reg = document.createElement('input');
		input_reg.id = options.popup_id +'_inp';
		input_reg.type = 'text';
		input_reg.autocomplete = 'off';
		input_reg.placeholder = 'Введите регион...';
		input_reg.setAttribute('class','search_edit_style');
		input_reg.setAttribute('onkeyup',"GenListRes("+gl+",'"+tag_id+"')");
		input_reg.setAttribute('autofocus','');
		
		div_border.appendChild(input_reg);
		wrapper.appendChild(div_border);
		wrapper.appendChild(div_for_resp);

        // center wrapper
        centerElement(wrapper);
        document.address_selector = wrapper;
		document.div_for_resp = div_for_resp;

        /*
         * Close selector
         */
        wrapper.closeSelector = function()
        {
            wrapper.parentNode.removeChild(wrapper);
            document.address_selector = false;
        }
		bind(input_reg,'blur', wrapper.closeSelector);

        return wrapper;
    }
	

	createWrapper();

}